<!-- 投诉及处置 -->
<template>
  <List
    :loading="loading"
    :total="total"
    :queryParams="queryParams"
    :list="list"
    :column="column"
    @handleChangeTime="handleChangeTime"
    @handleQuery="handleQuery"
    @handleSelectionChange="handleSelectionChange"
    @handleDelete="handleDelete"
  >
  </List>
</template>
<script setup>
defineOptions({
  name: "Complainthandling",
  inheritAttrs: false,
});

import List from "@/components/internal/list";
import { ElMessageBox, ElMessage } from "element-plus";
import { ref, reactive, onMounted } from "vue";

import ManageAPI from "@/api/manage";

const loading = ref(false);
const total = ref(0);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
});
// 接收组件传值
const activeTime = ref(1);

// 表格数据
const list = ref([]);
// 列字段
const column = ref([
  { label: "发生时间", prop: "record_date", type: "date" },
  { label: "标题", prop: "title", type: "input" },
  { label: "详情", prop: "details", type: "textarea" },
  { label: "附件", prop: "file_list", type: "upload" },
  { label: "备注", prop: "remark", type: "textarea" },
]);
// 批量勾选id
const selectIds = ref([]);

/** 查询 */
function handleQuery() {
  //   todo 查询列表接口
  loading.value = true;
  const { pageNum, pageSize } = queryParams;

  ManageAPI.queryInternal({
    type_: 4, //必传。类型:1-服务质量评价 2-执法过程评价 3-投诉处置评价 4-综合问卷
    filter_way: activeTime.value, //过滤类型：1-全部 2-最近一周 3-最近一个月 4-最近三个月 5-最近一年 缺省默认为1
    page: pageNum, //必传。当前页码，从1开始
    size: pageSize, //必传。每页显示条目数
  })
    .then((data) => {
      list.value = data.data;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
// 多选
function handleSelectionChange(selection) {
  selectIds.value = selection.map((item) => item.id);
}

// 切换时间
function handleChangeTime(value) {
  activeTime.value = value;
  handleQuery();
}

// 删除
function handleDelete(id) {
  const ids = [id || selectIds.value].join(",");
  if (!ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    () => {
      loading.value = true;
      ManageAPI.deleteInternal({
        id: ids,
      })
        .then((res) => {
          if (res.code === 200) {
            ElMessage.success("删除成功");
            handleQuery();
          } else {
            ElMessage.error(res.data);
          }
        })
        .finally(() => (loading.value = false));
    },
    () => {
      ElMessage.info("已取消删除");
    }
  );
}

onMounted(() => {
  handleQuery();
});
</script>
